"""
@author: haha
@file: news_data.py
@time: 2023/3/10 0010 21:13:30
"""
from flask import Blueprint, request
from flask_restful import Resource, Api
from model.news_data.news_data import Data
from model.user import StuInfo
from model import db
from libs.response import response_data
from libs.auth import login
import http.client, urllib, json

news_bp = Blueprint("news_bp", __name__, url_prefix="/v1")
api = Api(news_bp)

class NewsData(Resource):
    def get(self):
        try:
            # news = Data.query.filter.all()
            # print(news)

            conn = http.client.HTTPSConnection('apis.tianapi.com')  # 接口域名
            params = urllib.parse.urlencode({'key': 'bb72fb028f56f83edc1adf4c46121723', 'num': '10'})
            headers = {'Content-type': 'application/x-www-form-urlencoded'}
            conn.request('POST', '/auto/index', params, headers)
            tianapi = conn.getresponse()
            result = tianapi.read()
            data = result.decode('utf-8')
            dict_data = json.loads(data)
            news_list = dict_data.get('result').get('newslist')

            return response_data(message="获取数据成功", data=news_list)
        except:
            return response_data(code=1, message="获取数据失败")
api.add_resource(NewsData, "/home")
